<?php
public function goods() {
    	$mer_id = $this->merchant_session['mer_id'];
    	$store_id = $_GET['store_id']?intval($_GET['store_id']):'';
    	if(!$store_id){
    		$this->error('请选择店铺');
    	}
		
		
		
    	// 所有商品列表
    	$goodsConditon['merId'] = $mer_id;
    	$goodsConditon['storeId'] = $store_id;
		if(IS_POST){
			$goodsConditon['goodsName']=$_POST["proname"];
			$this->assign('search',$_POST["proname"]);
		}
    	$goodsList = D('Waimai_goods')->get_all_goods($goodsConditon,'goods_id DESC');//WON添加倒序

    	if(count($goodsList)<1){
    		return;
    	}
    	$waimai_image_class = new waimai_image();
    	foreach ($goodsList['goods_list'] as $k=>$goodsInfo){
    		$tmp_pic_arr = explode(';', $goodsInfo['image']);
    		$goodsList['goods_list'][$k]['list_pic'] = $waimai_image_class->get_image_by_path($tmp_pic_arr[0], 's');
    	}
    	
    	$this->assign('goodsList',$goodsList);
    	$this->assign('store_id',$store_id);
    	$this->display();
    }
    
    // 修改商品
    public function goods_manage(){
    	$mer_id = $this->merchant_session['mer_id'];
    	$store_id = $_GET['store_id']?intval($_GET['store_id']):'';
    	if(!$store_id){
    		$this->error('请选择店铺');
    	}
    	// 所有的商品分类列表
    	$columnCondition['merId'] = $mer_id;
    	$columnCondition['store_id'] = $store_id;
    	$columnCondition['gcat_status'] = 1;
    	$category = D('Waimai_goods_category')->get_all_category($columnCondition);
 
   	 	if(!$category || count($category['category_list']) <1){
    		redirect(U('Waimai/product_category'),1,'暂无商品分类 请先去添加商品分类');
    	}
    	$this->assign('categoryList',$category);
    	
    	$storeInfo = D('Waimai_store')->field('tools_money_have')->where(array('store_id'=>$store_id,'mer_id'=>$mer_id))->find();
    	$this->assign('storeInfo',$storeInfo);
    	
    	$goodsId = isset($_GET['goods_id'])?intval($_GET['goods_id']):'';
    	if($goodsId){
    		$waimai_image_class = new waimai_image();
    		$goodCondition = array(
    			'goods_id' => $goodsId,
    			'mer_id' => $mer_id,
    			'store_id' => $_GET['store_id']
    		);
	    	$goodInfo = D('Waimai_goods')->where($goodCondition)->find();
	    	if($goodInfo['mer_id'] !== $mer_id){
	    		$this->error('非法操作');
	    	}
	    	$tmp_pic_arr = explode(';', $goodInfo['image']);
	    
	    	foreach($tmp_pic_arr as $key=>$value){
				$goodInfo['pic'][$key]['title'] = $value;
				$goodInfo['pic'][$key]['url'] = $waimai_image_class->get_image_by_path($value, 's');
			}
	    	
			//won规格
			$goodInfo['gjson']=json_decode($goodInfo['gjson']);
			$goodInfo['gweight']=json_decode($goodInfo['gweight']);
//			dump($goodInfo['gjson']);exit();
			
			//缩略图
			$upimg=D('upimg')->where(array("goods_id"=>$goodsId))->find();
			$goodInfo["upimg"]=$waimai_image_class->get_image_by_path($upimg['src'], '-1');
			$goodInfo["upimg_src"]=$upimg['src'];
//			dump($goodsId);exit();
	    	$this->assign('goodInfo',$goodInfo);
    	}
    	
    	$this->assign('store_id',$store_id);
    	$this->display();
    }
    
    public function save_goods(){
    	$mer_id = $this->merchant_session['mer_id'];
    	
    	if(IS_POST){
    		
//			dump($_POST);exit();
			
			//won处理规格，生成一个JSON
    		foreach($_POST["g_name"] as $i=>$vals){
    			$gnew[$i][]=$vals;
    		}
			foreach($_POST["g_price"] as $i=>$vals){
    			$gnew[$i][]=$vals;
    		}		
			
			foreach($_POST["weight"] as $i=>$vals){
    			$weight[$i][]=$vals;
    		}
			foreach($_POST["weight_g"] as $i=>$vals){
    			$weight[$i][]=$vals;
    		}
			
			
    		$store_id = intval($_POST['store_id'])?intval($_POST['store_id']):0;
			
			$upimg['src']=$_POST["upimg"];
    		$column['mer_id']  = $mer_id;
    		$column['gcat_id'] = intval($_POST['gcat_id']);
    		$column['name']  = $_POST['goodname'];
    		$column['unit']  = $_POST['unit'];
    		$column['price'] = floatval($_POST['price']);
    		$column['old_price'] = floatval($_POST['old_price']);
    		$column['vip_price'] = floatval($_POST['vip_price']);
    		$column['image'] = implode(';',$_POST['pic']);
    		$column['desc'] = $_POST['desc'];
			$column['gjson']=json_encode($gnew);//won添加规格
			$column['gweight']=json_encode($weight);//won重量
			$column['gbody'] = htmlspecialchars_decode($_POST['gbody']);//won添加商品详情
    		$column['sort'] = intval($_POST['cat_sort']);
    		$column['limit'] = intval($_POST['daylimit']);
    		$column['status'] = intval($_POST['iswrite']);
    		$column['create_time'] = $_SERVER['REQUEST_TIME'];
    		$column['last_time'] = $_SERVER['REQUEST_TIME'];
    		$column['store_id'] = $store_id;
    		$column['tools_price'] = floatval($_POST['tools_price']);
    		$column['newuser'] = floatval($_POST['newuser']);
			
//			dump($column);exit();
			
    		if($_POST['goods_id']){
    			$columnCondition['goods_id'] = $_POST['goods_id'];
    			$columnCondition['mer_id'] = $mer_id;
    			$columnCondition['store_id'] = $_POST['store_id'];

    			$result = D('waimai_goods')->where($columnCondition)->save($column);
    			if(!$result){
					$this->error($result['msg']);
				}else{
					//添加图片	
					$imgfind = D('upimg')->where(array("goods_id"=>$columnCondition['goods_id']))->find();
					if($imgfind){
						D('upimg')->where(array("goods_id"=>$columnCondition['goods_id']))->save($upimg);
					}else{
						$upimg['goods_id']=$columnCondition['goods_id'];
						D('upimg')->data($upimg)->add();
					}
				}
				
    		}else{
    			$result = D('waimai_goods')->data($column)->add();
				if(!$result){
					$this->error($result['msg']);
				}else{
					//添加图片	
					$upimg['goods_id']=$result;
					D('upimg')->data($upimg)->add();	
				}
    		}
			
			
    		if(!$result){
//  			dump($result);exit();
				$this->error($result['msg']);
			}
			
//			C('SHOW_PAGE_TRACE',true);
//			dump($result);exit();
//			$this->display();
			$this->success('保存成功',U('Waimai/goods',array('store_id'=>$store_id)));
    	}
    }
	public function store_ajax_upload_pic(){
		if ($_FILES['imgFile']['error'] != 4) {
            $img_mer_id = sprintf("%09d", $this->merchant_session['mer_id']);
            $rand_num = mt_rand(10, 99) . '/' . substr($img_mer_id, 0, 3) . '/' . substr($img_mer_id, 3, 3) . '/' . substr($img_mer_id, 6, 3);

            $upload_dir = './upload/waimai/' . $rand_num . '/';
            if (!is_dir($upload_dir)) {
                mkdir($upload_dir, 0777, true);
            }
            import('ORG.Net.UploadFile');
            $upload = new UploadFile();
            $upload->maxSize = $this->config['waimai_pic_size'] * 1024 * 1024;
            $upload->allowExts = array('jpg', 'jpeg', 'png', 'gif');
            $upload->allowTypes = array('image/png', 'image/jpg', 'image/jpeg', 'image/gif');
            $upload->savePath = $upload_dir;
            $upload->thumb = true;
            $upload->imageClassPath = 'ORG.Util.Image';
            $upload->thumbPrefix = 'm_,s_';
            $upload->thumbMaxWidth = $this->config['waimai_pic_width'];
            $upload->thumbMaxHeight = $this->config['waimai_pic_height'];
            $upload->thumbRemoveOrigin = false;
            $upload->saveRule = 'uniqid';
            if ($upload->upload()) {
                $uploadList = $upload->getUploadFileInfo();

                $title = $rand_num . ',' . $uploadList[0]['savename'];

                $group_image_class = new waimai_image();
                $url = $group_image_class->get_image_by_path($title, 's');

                exit(json_encode(array('error' => 0, 'url' => $url, 'title' => $title)));
            } else {
                exit(json_encode(array('error' => 1, 'message' => $upload->getErrorMsg())));
            }
        } else {
            exit(json_encode(array('error' => 1, 'message' => '没有选择图片')));
        }
	}
	
	public function store_ajax_del_pic(){
		$store_image_class = new waimai_image();
		$store_image_class->del_image_by_path($_POST['path']);
	}
	
    // 删除商品
 	public function  goods_del(){
 		$goods_id = intval($_GET['goods_id']);
 		$store_id = intval($_GET['store_id']);
     	$mer_id = $this->merchant_session['mer_id'];
     	if($goods_id && $store_id){
			$condition['goods_id'] = $goods_id;
			$condition['mer_id'] = $mer_id;
			$condition['store_id'] = $store_id;
			$now_goods = D('Waimai_goods')->field(true)->where($condition)->find();
			
			if(!empty($now_goods)){
				if(D('Waimai_goods')->where($condition)->delete()){
					$this->success('删除成功！');
				}else{
					$this->error('删除失败！请重试~');
				}
			}else{
				$this->error('非法提交,请重新提交~！');
			}
		}else{
			$this->error('非法提交,请重新提交~');
		}
 	}
?>